package com.letou.ls.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.table.PreferencesUtils;
import org.apache.table.TableTitle;

import com.letou.ls.R;
import com.letou.ls.mode.AppConfigInfo;
import com.letou.ls.mode.LotteryInfo;
import com.letou.ls.uitl.AppConfigManager;
import com.letou.ls.uitl.Constant;
import com.letou.ls.view.TableBadyView;
import com.letou.ls.view.TableBottmBady;
import com.letou.ls.view.TableBottomTile;
import com.letou.ls.view.TableBottomTileHappyTen;
import com.letou.ls.view.TableTitleViewHappy;
import com.letou.ls.view.TableViewUtil;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 黑龙江快乐十分
 *
 * @author libin
 *
 *         2016年6月27日
 */
public class HappyTenActivity extends BaseActivity {
    private LinearLayout mTopLayout;
    private LinearLayout mBottomLayout;
    private LinearLayout mContextLayout;
    private TableBadyView mTableBadyView;
    private float textSize;
    private float itemHeight;
    private ArrayList<Float> widthList;
    private TableBottmBady mTableBottmBady;
    private ScrollView scrollview;
    private TextView bottomText;
    
    private LinearLayout menuParent;
    private ImageButton menuBtn;
    private TextView startLotto;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	 // 添加 标记
        lotteryCode = Constant.LOTTERY_CODE_01;
        screen_direction_type = 0;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_happyten);

        initFirstDateTime("8:44:59", "8:55:00");

        Log.e("lb", "onCreate  lotteryCode:" + lotteryCode);
        mTopLayout = (LinearLayout) findViewById(R.id.happyten_top_layout);
        mContextLayout = (LinearLayout) findViewById(R.id.happyten_context_layout);
        mBottomLayout = (LinearLayout) findViewById(R.id.happyten_bottom_layout);
        scrollview = (ScrollView) findViewById(R.id.happyten_scrollview);

        bottomText = (TextView) findViewById(R.id.happyten_bottom_text);
        menuParent = (LinearLayout) findViewById(R.id.menuParent);
        menuBtn = (ImageButton) findViewById(R.id.bottom_menu_bt);

        startLotto = (TextView) findViewById(R.id.startLotto);

        // widthList = title.getmWidthList();
        initWidthList();
      

        TableTitle title = TableViewUtil.getHappy10Table(this, viewWidth);

        // 标题头
        TableTitleViewHappy tableTopTitle = new TableTitleViewHappy(this, widthList);
        LinearLayout.LayoutParams ll =
                new LinearLayout.LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);
        tableTopTitle.addData(title, density);
        tableTopTitle.setLayoutParams(ll);

        textSize = title.getDataTextSize();

        mTableBadyView = new TableBadyView(this, widthList);

        // itemHeight = textSize * 1.618f * density;
        itemHeight = widthList.get(3);

        int size = 50;
        int tableHeight = (int) (itemHeight * size + size + 1);

        LinearLayout.LayoutParams badyParams =
                new LinearLayout.LayoutParams(viewWidth, tableHeight);
        mTableBadyView.setLayoutParams(badyParams);

        // 底部标题头
        TableBottomTileHappyTen tableBottom = new TableBottomTileHappyTen(this);
        tableBottom.setmWidthList(widthList);
        tableBottom.addData(title, density);
        tableBottom.setLayoutParams(ll);

        mTopLayout.addView(tableTopTitle);
        mContextLayout.addView(mTableBadyView, badyParams);
        mBottomLayout.addView(tableBottom);

        mTableBottmBady = new TableBottmBady(this);
        mTableBottmBady.setLayoutParams(ll);

        mBottomLayout.addView(mTableBottmBady);

        scrollview.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                
                // Logger.v("TEST", " scrollview " + arg1.getAction());
                return mGesture.onTouchEvent(arg1);
                // TODO Auto-generated method stub
            }

        });
        String dateline = "";
        if (Constant.getCurrentLottery() != null) {
            dateline = "       截止日期:" + Constant.getExpirationTime();
            dateline = dateline + "       站号:"
                    + PreferencesUtils.getString(this, Constant.PRE_TAG_USERNAME);
            dateline = dateline + "       版本号:" + Constant.getCurrentVersion(this);
        }
        String text = "     开奖信息以彩票中心为准，此信息仅供参考！ " + dateline;
        bottomText.setText(text);
        menuBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                gotoMenuActivity();
            }

        });
    }
    
    private void initWidthList() {
    	  AppConfigInfo info = AppConfigManager.getInstance(HappyTenActivity.this).getAppConfigInfo();
          // 必选 期号
          // 互斥 前三直 和 开奖号码互斥 2位 和 3位 显示折线 和 开奖号码分布
          // 至少选1个 开奖号码分布 和 前三分布
          // 可选 和值 跨度 大号个数 单号个数 推荐号 遗漏值 出现次数
          Log.e("cc", "initWidthList:" + info.toString());
          int count = 28;
          int lineCount = 0;
          if (info.getKuadu()) {// 跨度
              count += 1;
          }
          if (info.getHezhi()) {// 和值
              count += 1;
          }
          if (!info.getKuadu() && !info.getHezhi()) {
              lineCount = 1;
          }
          Log.e("cc", "initWidthList:count:" + count + ";lineCount:" + lineCount + ";viewWidth:"
                  + viewWidth);
          widthList = new ArrayList<Float>();
          float item = (viewWidth - lineCount) / (count);

          int i = 0;
          for (i = 0; i < 30; i++) {
              switch (i) {
                   case 0:
                	   widthList.add(item * 3-1);
                	   break;
                  case 24:
                      if (info.getKuadu()) {// 跨度
                          widthList.add(item-1 );
                      } else {
                          widthList.add(item * 0);
                      }

                      break;
                  case 25:
                      if (info.getHezhi()) {// 和值
                          widthList.add(item-1 );
                      } else {
                          widthList.add(item * 0);
                      }
                      break;
                
                  
                  default:
                      widthList.add(item-1);
                      break;
              }
          }
    	
    	
    	
//    	  widthList = new ArrayList<Float>();
//          int i = 0;
//          Log.e("lb", "宽度分组:" + viewWidth + ";" + viewWidth % 30 + ":" + viewWidth % 32);
//          if (viewWidth % 30 == 0) {
//              for (i = 0; i < 28; i++) {
//                  if (i == 0) {
//                      widthList.add((float) ((viewWidth) / 30 * 2.5 - 1));
//                  } else if (i == 25) {
//                      widthList.add((float) ((viewWidth) / 30 * 1.5 - 1));
//                  } else {
//                      widthList.add((float) ((viewWidth) / 30 - 1));
//                  }
//              }
//          } else if (viewWidth % 32 == 0) {
//              for (i = 0; i < 28; i++) {
//                  if (i == 0) {
//                      widthList.add((float) ((viewWidth) / 32 * 3 - 1));
//                  } else if (i == 24 || i == 25 || i == 26 || i == 27) {
//                      widthList.add((float) ((viewWidth) / 32 * 1.5 - 1));
//                  } else {
//                      widthList.add((float) ((viewWidth) / 32 - 1));
//                  }
//              }
//          }
//		
	}

	@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        bottomText.requestFocus();
    }
    
    @Override
    protected void updateView(List<LotteryInfo> lotteryInfo, String miss, String sum) {
        if (lotteryInfo.isEmpty()) {
            return;
        }
        ArrayList<ArrayList<ArrayList<String>>> data =
                new ArrayList<ArrayList<ArrayList<String>>>();
        int size = lotteryInfo.size();
        
        for (int index = 0; index < size; index++) {
            
            ArrayList<ArrayList<String>> itemList = new ArrayList<ArrayList<String>>();
            LotteryInfo info = lotteryInfo.get(index);
            String value = info.getLotteryNumbers();
            String[] array = value.split(",");
            int arraySize = array.length;
            int[] all = new int[arraySize];
            int count = 0;
            for (int j = 0; j < arraySize; j++) {
                all[j] = Integer.valueOf(array[j]);
                count += all[j];
            }
            int[] aa = {all[0], all[1], all[2]};
            if (isFirstData(info.getDateline())) {
                TableViewUtil.addCol(info.getIssue(), 4, itemList);// 1
            } else {
                TableViewUtil.addCol(info.getIssue(), 0, itemList);// 1
            }
            TableViewUtil.addCol(array[0], 2, itemList);// 2
            TableViewUtil.addCol(array[1], 2, itemList);// 3
            TableViewUtil.addCol(array[2], 2, itemList);// 4
            TableViewUtil.getDataArray(aa, all, itemList, 20);
            
            TableViewUtil.addCol("" + (all[array.length - 1] - all[0]), 3, itemList);// 25
            TableViewUtil.addCol("" + count, 3, itemList);// 26
            TableViewUtil.addCol(getBigSmall(all, 11), 3, itemList);// 27
            TableViewUtil.addCol(getjiou(all), 3, itemList);// 28
            
            data.add(itemList);
        }
        mTableBadyView.updateData(data, widthList, TableViewUtil.redLine_happyTen,
                (textSize + 2) * density, itemHeight);
        
        ArrayList<ArrayList<String>> bottomdata = new ArrayList<ArrayList<String>>();
        ArrayList<String> bottomitem = new ArrayList<String>();
        bottomitem.add("当前遗漏值");
        String[] missArray = miss.split(",");
        size = missArray.length;
        for (int index = 0; index < size; index++) {
            bottomitem.add(missArray[index]);
        }
        
        bottomdata.add(bottomitem);
        bottomitem = new ArrayList<String>();
        bottomitem.add("出现总次数");
        String[] sumArray = sum.split(",");
        size = sumArray.length;
        for (int index = 0; index < size; index++) {
            bottomitem.add(sumArray[index]);
        }
        bottomdata.add(bottomitem);
        
        ArrayList<Float> list = TableViewUtil.getBottomWidth(widthList, 4, 24, 27);
        mTableBottmBady.addData(bottomdata, TableViewUtil.redL_happyTen_bo, list, textSize,
                R.drawable.icon_happy10);
        
        scrollview.post(new Runnable() {
            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                
                scrollview.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
        // bottomText.requestFocus();
        
        LayoutParams lp = startLotto.getLayoutParams();
        lp.width = list.get(0).intValue() - 1;
        startLotto.setLayoutParams(lp);
        
        lp = menuParent.getLayoutParams();
        float pw = lp.width = list.get(list.size() - 1).intValue()*4;
        menuParent.setLayoutParams(lp);
        
        lp = menuBtn.getLayoutParams();
        Drawable bg = menuBtn.getBackground();
        lp.height = (int) (pw / bg.getIntrinsicWidth() * bg.getIntrinsicHeight());
        menuBtn.setLayoutParams(lp);
    }
    
    @Override
    public void updateTimeCount(String time) {
        if(!TextUtils.isEmpty(time)){
        	 startLotto.setText(time);
        }else{
        	 startLotto.setText(mCountdownTime);	
        }
       
    }
    
}
